package com.app.xinfadi.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;

import com.app.xinfadi.green_dao.dao.DaoMaster;
import com.app.xinfadi.green_dao.dao.DaoSession;
import com.app.xinfadi.green_dao.dao.ProductBeanDao;
import com.app.xinfadi.green_dao.model.ProductBean;
import com.app.xinfadi.util.LogUtil;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;


/**
 * 数据库基础类
 *
 * @author WuTengFei
 */
public class DBHelper {

    private static final String TAG = "DBHelper";
    public static DBHelper singleton;
    private Context context = null;
    private DaoMaster.DevOpenHelper dbHelper = null;
    private SQLiteDatabase db = null;
    private Cursor cursor;

    /**
     * 数据库名称
     */
    private static final String DATABASE_NAME = "xinfadi";
    /**
     * 数据库版本
     */
    private static final int DATABASE_VERSION = 1;
    private DaoMaster daoMaster;
    private DaoSession daoSession;

    public synchronized static DBHelper getInstance(Context context) {
        if (singleton == null) {
            synchronized (DBHelper.class) {
                if (singleton == null) {
                    singleton = new DBHelper(context);
                    singleton.dbHelper = new DaoMaster.DevOpenHelper(context, DATABASE_NAME, null);
                    try {
                        singleton.db = singleton.dbHelper.getWritableDatabase();
                    } catch (Exception ex) {
                        try {
                            singleton.db = singleton.dbHelper.getReadableDatabase();
                        } catch (Exception e) {
                            LogUtil.e(TAG, e + "");
                        }
                    }
                    singleton.daoMaster = new DaoMaster(singleton.db);
                    singleton.daoSession = singleton.daoMaster.newSession();
                }
            }
        }
        return singleton;
    }

    public DBHelper(Context context) {
        this.context = context;
    }

    public void saveProducts(List<ProductBean> productBeanList) {
        db.beginTransaction();
        try {
            ProductBeanDbHelper.getInstance(context).deleteAll();
            for (ProductBean productBean : productBeanList) {
                ProductBeanDbHelper.getInstance(context).insert(productBean);
            }

            db.setTransactionSuccessful();
        } catch (Exception e) {
            LogUtil.e(TAG, e + "");
        } finally {
            db.endTransaction();
        }

    }
    /**
     * 打开数据库
     */
//    public void open() {
//        LogUtil.d(TAG, "open()");
//        dbHelper = new DaoMaster.DevOpenHelper(context, DATABASE_NAME, null);
////		dbHelper = new DataBaseHelper(context);
//        try {
//            db = dbHelper.getWritableDatabase();
//        } catch (Exception ex) {
//            try {
//                db = dbHelper.getReadableDatabase();
//            } catch (Exception e) {
//                LogUtil.e(TAG, e + "");
//            }
//        }
//        daoMaster = new DaoMaster(db);
//        daoSession = daoMaster.newSession();
//    }

    /**
     * 关闭数据库
     */
    public void close() {
        if (dbHelper != null) {
            dbHelper.close();
        }
    }

    /**
     * 获取数据库操作对象
     *
     * @return SQLiteDatabase
     */
    public SQLiteDatabase getSQLiteDatabase() {
        return db;
    }

    /**
     * 关闭cursor
     */
    private void CloseCursor() {
        if (cursor != null) {
            cursor.close();
        }
    }

    /**
     * 初始化数据
     */
    @Deprecated
    public void initData() {
        db.beginTransaction();
        try {
//			DepartmentDbHelper.getInstance().executeAssetsSQL(db);
//			HospitalDbHelper.getInstance().executeAssetsSQL(db);
//			DeliveryDbHelper.getInstance().executeAssetsSQL(db);
            db.setTransactionSuccessful();
        } catch (Exception e) {
            LogUtil.e(TAG, e + "");
        } finally {
            db.endTransaction();
//			db.close();
        }

    }

    public DaoMaster getDaoMaster() {
        return daoMaster;
    }

    public DaoSession getDaoSession() {
        return daoSession;
    }

    /**
     * baseHelper
     *
     * @author WuTengFei
     */
    @Deprecated
    public class DataBaseHelper extends SQLiteOpenHelper {
        private static final String TAG = "DataBaseHelper";

        public DataBaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.beginTransaction();
            try {
                LogUtil.d(TAG, "DATA base oncreate");
//				db.execSQL(DepartmentDbHelper.TABLE_CREATE_SQL);
//				db.execSQL(HospitalDbHelper.TABLE_CREATE_SQL);
//				db.execSQL(DeliveryDbHelper.TABLE_CREATE_SQL);
//				db.execSQL(ReservationNewsDbhHelper.TABLE_CREATE_SQL);
//				db.execSQL(SystemInformationDbhHelper.TABLE_CREATE_SQL);
//				db.execSQL(PatientmanOftenDbHelper.TABLE_CREATE_SQL);
//				db.execSQL(ThirdPartyAccountDbHelper.TABLE_CREATE_SQL);
//				db.execSQL(DoctorAppointmentDbHelper.TABLE_CREATE_SQL);
                db.setTransactionSuccessful();
            } catch (Exception e) {
                LogUtil.e(TAG, e + "");
            } finally {
                db.endTransaction();
            }
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            onCreate(db);
        }

        @Override
        public void close() {
            try {
                db.close();
            } catch (Exception e) {
                LogUtil.e(TAG, e.getStackTrace() + "");
            }
        }

    }
}
